package com.dldata.devtools30.utils.schduletask;

import com.dldata.devtools30.utils.Public;
import com.dldata.devtools30.utils.StringUtil;
import com.dldata.devtools30.utils.filelistener.FileListenerCommon;
import com.dldata.devtools30.utils.filelistener.FileObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by zhangyy on 2017-05-02.
 */
public class ConfigFileObserver implements  Runnable {

    public static final String DATACONFIG_PROPERTIES = "dataconfig.properties";
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    FileObserver fileObserver;

    public ConfigFileObserver(String dir){
//        FileObserver ob = new FileObserver("D:\\test");
//        FileListener listener = new FileListener();
//        ob.addListener(listener);
//        FileMonitorImpl monitor = new FileMonitorImpl(ob);
//        monitor.start();
        fileObserver = new FileObserver(new File(dir),new FileFilter(){
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(DATACONFIG_PROPERTIES);
            }
        });
        fileObserver.addListener(new FileListener());
        try {
            fileObserver.initialize();
            loadFile(new File(dir+"/"+DATACONFIG_PROPERTIES));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void run() {
        try{
            innerRun();
        }catch(Throwable e){
            logger.error(e.getMessage(),e);
        }
    }

    private void innerRun() {
         fileObserver.checkAndNotify();
    }


    private class FileListener extends FileListenerCommon {

        @Override
        public void onFileChange(File file) {
            loadFile(file);
        }

    }

    private void loadFile(File file) {
        Properties properties = new Properties();
        InputStream ins = null;
        try {
            ins = new FileInputStream(file);
            properties.load(ins);

            /**
             * 验证码有效时长 秒 60*3
             */
            if(!StringUtil.isEmptyOrLength0(properties.getProperty("SMSCODEVALIDTIMELEN"))){
                Public.SMSCODEVALIDTIMELEN=Integer.parseInt(properties.getProperty("SMSCODEVALIDTIMELEN"));
                logger.info("Public.SMSCODEVALIDTIMELEN: "+ Public.SMSCODEVALIDTIMELEN);
            }

            /**
             *  token有效时长 秒 86400 * 3
             */
            if(!StringUtil.isEmptyOrLength0(properties.getProperty("TOKEN_VALIDTIMELEN"))){
                Public.TOKEN_VALIDTIMELEN=Integer.parseInt(properties.getProperty("TOKEN_VALIDTIMELEN"));
                logger.info("Public.TOKEN_VALIDTIMELEN: "+ Public.TOKEN_VALIDTIMELEN);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            if(ins!=null)
            try {
                ins.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
